package com.ez.supervise.action.base;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;

public class UploadFileAction extends BaseAction {

    private static final long serialVersionUID = 1L;

    private static final int BUFFER_SIZE = 16 * 1024;
    private String title;
    private File[] upload;
    private String[] uploadFileName;
    private String[] uploadContentType;
    private String savePath;

    private static boolean copy(File src, File dst) {
	boolean result = false;
	InputStream in = null;
	OutputStream out = null;
	try {
	    in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
	    out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
	    byte[] buffer = new byte[BUFFER_SIZE];
	    int len = 0;
	    while ((len = in.read(buffer)) > 0) {
		out.write(buffer, 0, len);
	    }
	    result = true;
	} catch (Exception e) {
	    e.printStackTrace();
	    result = false;
	} finally {
	    if (null != in) {
		try {
		    in.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	    if (null != out) {
		try {
		    out.close();
		} catch (IOException e) {
		    e.printStackTrace();
		}
	    }
	}
	return result;
    }

    @Override
    public String execute() throws Exception {
	File[] srcFiles = this.getUpload();
	List<String> successFileList = new ArrayList<String>();
	if (null != srcFiles)
	    for (int i = 0; i < srcFiles.length; i++) {
		String dstPath = ServletActionContext.getServletContext().getRealPath(getSavePath()) + File.separator +
			this.getUploadFileName()[i];
		File dstFile = new File(dstPath);
		if (copy(srcFiles[i], dstFile)) {
		    successFileList.add(getUploadFileName()[i]);
		}
	    }
	if (successFileList.size() == 1)
	    return successFileList.get(0);
	else if (successFileList.size() == 0)
	    return null;
	else {
	    ServletActionContext.getRequest().setAttribute("successFileList", successFileList);
	    return SUCCESS;
	}
    }

    public String getTitle() {
	return title;
    }

    public void setTitle(String title) {
	this.title = title;
    }

    public File[] getUpload() {
	return upload;
    }

    public void setUpload(File[] upload) {
	System.out.println("upload ====" + upload);
	this.upload = upload;
    }

    public String[] getUploadFileName() {
	return uploadFileName;
    }

    public void setUploadFileName(String[] uploadFileName) {
	this.uploadFileName = uploadFileName;
    }

    public String[] getUploadContentType() {
	return uploadContentType;
    }

    public void setUploadContentType(String[] uploadContentType) {
	this.uploadContentType = uploadContentType;
    }

    public String getSavePath() {
	return savePath;
    }

    public void setSavePath(String savePath) {
	this.savePath = savePath;
    }
}
